﻿using SchoolCommunicationSystem.Models;
using SCS.Business;
using System.Net;
using System.Web.Mvc;
namespace SchoolCommunicationSystem.Controllers
{
    public class SMSController : Controller
    {
        SMSServices _smsServices = new SMSServices();
        ClassServices _classServices = new ClassServices();
        //
        // GET: /SMS/

        /// <summary>
        /// Index
        /// </summary>
        /// <returns>View</returns>
        public ActionResult Index()
        {
            ClassToSMS model = new ClassToSMS();
            model.AllClass = _classServices.GetAllClassInSchoolYear(1);
            return View(model);
        }

        /// <summary>
        /// Pre SendSMS
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns>View</returns>
        public ActionResult SendSMS(int id)
        {
            SMSClassView model = new SMSClassView();
            model.SMS = _smsServices.GetSMSAClass(id);
            return View(model);
        }

        /// <summary>
        /// SendSMS
        /// </summary>
        /// <param name="model">SMSClassView</param>
        /// <returns>View</returns>
        [HttpPost]
        public ActionResult SendSMS(SMSClassView model)
        {
            string userapi = "fuscscom";
            string passwordapi = "aguda5uN";
            string smsCode = "38745370437047";
            string api = "http://netvn.vn/apis/send_sms?smsUser={0}&smsPass={1}&smsNumber={2}&smsContent={3}&smsCode={4}";
            string ouput = string.Format(api, userapi, passwordapi, model.SMS[0].phone, model.SMS[0].smsContent, smsCode);
            //        HttpClient client = new HttpClient();

            //          var content = new StringContent("Some text");
            //         var response = client.GetStringAsync(ouput).Result;

            // Create a new request to the above mentioned URL. 
            WebRequest myWebRequest = WebRequest.Create(ouput);
            WebResponse myWebResponse = myWebRequest.GetResponse();
            var a = myWebResponse.ResponseUri;
            myWebResponse.Close();
            //  HttpClient client = new HttpClient();
            //        client.BaseAddress = new Uri("http://localhost:8080/");

            // HttpResponseMessage response = client.GetAsync(ouput).Result;
            //  string json = response.Content.ReadAsStringAsync();
            //   if (response.IsSuccessStatusCode)
            //   {
            // How do I get the JSON string out of the response object?
            //ret = response.??
            //   }
            //   else
            //   {
            //   }
            return View(model);
        }
    }
}
